package com.srs.dao.info;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.info.AgentPercent;
import com.srs.util.HibernateUtil;

public class AgentPercentDao {

	final static Logger logger = LoggerFactory.getLogger(AgentPercentDao.class);

	private List<AgentPercent> agentPercentList;
	private AgentPercent agentPercent;

	public void delete(Integer id) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			agentPercent = (AgentPercent) session.get(AgentPercent.class, id);
			session.delete(agentPercent);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<AgentPercent> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			agentPercentList = session.createQuery("from User").list();
			return agentPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<AgentPercent> list(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from AgentPercent ap where ap.onDate=:onDate");
			q.setString("onDate", onDate);
			agentPercentList = q.list();
			return agentPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}	
	
	@SuppressWarnings("unchecked")
	public List<AgentPercent> list(String onDate, Integer scopeId, Integer scopeValueId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q;
			if (scopeId==0){
				q = session.createQuery("from AgentPercent ap where ap.onDate=:onDate");
				q.setString("onDate", onDate);
			} else {
				q = session.createQuery("from AgentPercent ap where ap.onDate=:onDate and ap.scopeId=:scopeId and ap.scopeValueId=:scopeValueId");
				q.setString("onDate", onDate);
				q.setInteger("scopeId", scopeId);
				q.setInteger("scopeValueId", scopeValueId);
			}
			agentPercentList = q.list();
			return agentPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}		
	
	@SuppressWarnings("unchecked")
	public List<AgentPercent> list(String onDate, Integer cellId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from AgentPercent ap where ap.onDate=:onDate and ap.cellId=:cellId");
			q.setString("onDate", onDate);
			q.setInteger("cellId", cellId);
			agentPercentList = q.list();
			return agentPercentList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}		
	
	public AgentPercent get(Integer id) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.uid=:id");
			q.setInteger("userid", id);
			return (AgentPercent) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(AgentPercent agentPercent) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(agentPercent);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(AgentPercent agentPercent) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(agentPercent);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public AgentPercent get(String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from AgentPercent ap where ap.onDate=:onDate");
			q.setString("onDate", onDate);
			return (AgentPercent) q.uniqueResult();
		} finally {
			session.close();
		}

	}	

	public static void main(String[] args) {
		String a ="2009-02-02";
		String b ="2009-02-03";
		System.out.println(b.compareToIgnoreCase(a));
	} 
	
}
